﻿Shader "Unlit/BrightnessSaturationAndContrast"
{
    Properties
    {
        _MainTex ("Base(RGB)", 2D) = "white" {}
        _Brightness("Brightness（亮度）",Float) = 1
        _Saturation("Saturation（饱和度）",Float) = 1
        _Contrast("Contrast（对比度）",Float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            ZTest Always Cull Off ZWrite off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            half _Brightness;
            half _Saturation;
            half _Contrast;

            struct a2v
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                half2 uv : TEXCOORD0;
            };

            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 renderTex = tex2D(_MainTex, i.uv);
                //亮度
                fixed3 finalColor = renderTex.rgb * _Brightness;
                //饱和度 将颜色分量乘以特定系数、再相加 => 饱和度为0的颜色值
                fixed luminance = 0.2125 * renderTex.r + 0.7154 * renderTex.g + 0.0721 * renderTex.b;
                fixed3 luminanceColor = fixed3(luminance,luminance,luminance);
                finalColor = lerp(luminanceColor,finalColor,_Saturation); //饱和度为0 到 当前颜色进行插值
                //对比度
                fixed3 avgColor = fixed3(0.5,0.5,0.5); //对比度为0的颜色值
                finalColor = lerp(avgColor,finalColor,_Contrast); //对比度为0 到 当前颜色进行插值
                return fixed4(finalColor,renderTex.a);
            }
            ENDCG
        }
    }
    FallBack Off
}
